<?php

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");
require_once("lib/exception/sql_exception.php");

require_once("util/connection.php");

require_once("model/article_article_category/to/article_article_category_to.php");
require_once("model/article_article_category/dao/article_article_category_dao.php");


class Mysql_article_article_category_dao extends Article_article_category_dao {

	public function create($article_number, $article_category_number) {
		if ($this->exist($article_number, $article_category_number)) {
			throw new Duplicate_instance_exception(get_class($this));
		}
		try {
			$link = Connection::singleton()->transaction_begin();

			$result = mysql_query("insert into article_article_category	
									(article_number, article_category_number) values 
									('$article_number', '$article_category_number')",$link);
																	
			if (!$result || (mysql_affected_rows($link) != 1)) 
					throw new Sql_exception("Not row inserted in " .get_class($this));
			
			Connection::singleton()->transaction_commit($link);
			
			$article_article_category_to = new Article_article_category_to($article_number, $article_category_number);
			return $article_article_category_to;
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	public function exist($article_number, $article_category_number){
		try {
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_article_category
									where article_number='$article_number' and
									article_category_number='$article_category_number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;								
			}

			Connection::singleton()->transaction_commit($link);
			return true;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	public function find_by_number_and_category_article($article_number, $article_category_number) {
		try {
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_article_category
									where article_number='$article_number' and
									article_category_number='$article_category_number'", $link);
									
			if (mysql_num_rows($result) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_num_rows($result) != 1) {
				throw new Sql_exception(get_class($this));
			}
			
			$aux = mysql_fetch_assoc($result);			
			Connection::singleton()->transaction_commit($link);

			return new Article_article_category_to($aux['article_number'], $aux['article_category_number']);
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	public function find_by_number_article($article_number){
		try {
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select article_category_number from article_article_category
									where article_number='$article_number'", $link);
									
			if (mysql_num_rows($result) == 0) {
				throw new Instance_not_found_exception(get_class($this));
			}									
			
			$i = 0;
			$category_list = NULL;
			
			while ($article_category_number = mysql_fetch_assoc($result))
				$category_list[$i++]=$article_category_number['article_category_number'];
			
			Connection::singleton()->transaction_commit($link);				
			return $category_list;
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}	
	}
			
	public function remove($article_number, $article_category_number) {
		try {
			 $link = Connection::singleton()->transaction_begin();
			 
			 $result = mysql_query("delete from article_article_category
			 						where article_number='$article_number' and
									article_category_number='$article_category_number'", $link);
			 
			if (mysql_affected_rows($link) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_affected_rows($link) != 1) 
				throw new Sql_exception("Not row removed in " . get_class($this));		
			
			Connection::singleton()->transaction_commit($link);
			
		}
	catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);		
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
}
?>